package com.strawcom.charmring.util;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.content.res.AssetManager;

public class GetInputStream {

public static InputStream getInputStream(Context context, String httpgeturl){
		
		AssetManager am = context.getAssets();
		InputStream ins = null;
		InputStream is = null;
		try {
				ins = am.open("tomcat.cer");
		        //读取证书
		        CertificateFactory cerFactory = CertificateFactory.getInstance("X.509");
		        Certificate cer = cerFactory.generateCertificate(ins);
		        //创建一个证书库，并将证书导入证书库
		        KeyStore keyStore = KeyStore.getInstance("PKCS12", "BC");
		        keyStore.load(null, null);
		        keyStore.setCertificateEntry("trust", cer);

				//把咱的证书库作为信任证书库
				SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore);
				Scheme sch = new Scheme("https", socketFactory, 443);
				//完工
				HttpClient httpclient = new DefaultHttpClient();
				httpclient.getConnectionManager().getSchemeRegistry().register(sch);
				
				HttpGet httpget = new HttpGet(httpgeturl);
				
				System.out.println("12");
				HttpResponse response = httpclient.execute(httpget);
				System.out.println("13");
				
				HttpEntity entity = response.getEntity();
				
	            if (entity != null) {              
	                //start 读取整个页面内容  
	                is = entity.getContent(); 
	            }
				
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		        try {
					ins.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		
		System.out.println("is = " + is);
		
		return is;
		
	}
	
}
